/**
 * \file chunk.h
 * \brief Description of the chunk data structure, a buffer that
 * can grow when needed.
 **/

#ifndef R_CHUNK_H
#define R_CHUNK_H

#include "standard.h"

/**
 * \brief The chunk data structure. This is a buffer that can expand
 * when neccessary to contain data. It can expand in two regimes:
 *  - Grow, exactly, to the given size.
 *  - Grow with care for asymptotic performance (by doubling in size).
 * You should always use a Chunk*, Chunk cannot exist on the stack.
 * You should always use the r_chunk_data() macro.
 **/
struct ChunkT{
  uint32_t size;
  uint32_t alloc_size;
};
typedef struct ChunkT Chunk;

/**
 * \brief Ensure the chunk is of the given size. Grow exactly to the wanted
 * size.
 **/
Chunk* r_chunk_ensure(Chunk* chunk, size_t size);

/**
 * \brief Ensure the chunk is of the given size. Grow with good asymptotic
 * performance.
 **/
Chunk* r_chunk_ensure_a(Chunk* chunk, size_t size);

/**
 * \brief Create a new chunk of exact size.
 **/
Chunk* r_chunk_new(size_t size);

#endif /* R_CHUNK_H */
